﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Spymaster.Entities.Components;
using Spymaster.Screens;

namespace Spymaster.Entities
{
    public class PlayerEntity :
        GameEntity
    {
        private Vector2 lookTarget;
        private Vector2 mouseTarget;
        private Vector2 oldPosition;
        
        private float playerSpeed = 100.0f;
        public float PlayerSpeed
        {
            get { return playerSpeed; }
        }

        private KeyboardComponent keyboard;
        private MouseComponent mouse;
        private AimComponent aim;
        private RenderComponent render;

        private GameScreen screen;

        public PlayerEntity(GameScreen screen)
        {
            this.screen = screen;
        }

        public override void Initialize()
        {
            Position = new Vector2(0, 0);

            keyboard = new KeyboardComponent(this);
            mouse = new MouseComponent(this);
            aim = new AimComponent(this);
            render = new RenderComponent(this);

            oldPosition = Position;
        }

        public override void Update(GameTime gameTime)
        {


            keyboard.Update(gameTime);
            mouse.Update();
            //Console.WriteLine(mouse.GetMousePosition());
            lookTarget = Position -
                mouse.GetMousePosition() + 
                new Vector2((screen.ScreenWidth / 2.0f) - Position.X,
                    (screen.ScreenHeight / 2.0f) - Position.Y);
            mouseTarget = lookTarget - oldPosition;
            aim.Update(lookTarget);

            oldPosition = Position;
            //Console.WriteLine(Position);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            render.Draw(gameTime, spriteBatch);
        }

        public void FireWeapon()
        {
            Console.WriteLine(Position);
            PlayerBulletEntity bullet = new PlayerBulletEntity(Position, Rotation);
            
            screen.AddEntity(bullet);
        }
    }
}
